const util = require('../../utils/util');
const VendorService = require('../../service/admin/basic_vendor.service');

async function createVendor(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { vName, remark, state, orgName, taxNum, bankName, bankCard, billInfo, email, address, phone, grade } = ctx.request.body;

  if (!vName) {
    return util.fail(ctx, '供应商名称不能为空');
  }

  try {
    const result = await VendorService.createVendor({
      vName,
      remark,
      state,
      orgName,
      taxNum,
      bankName,
      bankCard,
      billInfo,
      email,
      address,
      phone,
      grade,
      create_id: userId,
      updata_id: userId
    });
    util.success(ctx, { id: result.id });
  } catch (error) {
    console.error('Create vendor error:', error.message);
    util.fail(ctx, error.message || '创建失败');
  }
}

async function deleteVendors(ctx) {
  const { ids } = ctx.request.body;

  if (!Array.isArray(ids) || ids.length === 0) {
    return util.fail(ctx, '供应商 ID 列表无效');
  }

  try {
    await VendorService.deleteVendors(ids);
    util.success(ctx, '');
  } catch (error) {
    console.error('Delete vendors error:', error.message);
    util.fail(ctx, error.message || '删除失败');
  }
}

async function updateVendor(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { id, vName, remark, state, orgName, taxNum, bankName, bankCard, billInfo, email, address, phone, grade } =
    ctx.request.body;

  if (!id || !vName) {
    return util.fail(ctx, '供应商 ID 或名称不能为空');
  }

  try {
    await VendorService.updateVendor({
      id,
      vName,
      remark,
      state,
      orgName,
      taxNum,
      bankName,
      bankCard,
      billInfo,
      email,
      address,
      phone,
      grade,
      updata_id: userId
    });
    util.success(ctx, '');
  } catch (error) {
    console.error('Update vendor error:', error.message);
    util.fail(ctx, error.message || '更新失败');
  }
}

async function getVendorList(ctx) {
  const { page = 1, limit = 10, vName, state, phone, grade } = ctx.query;

  try {
    const result = await VendorService.getVendorList({
      page: parseInt(page),
      limit: parseInt(limit),
      vName,
      state: state ? parseInt(state) : undefined,
      phone,
      grade: grade ? parseInt(grade) : undefined
    });
    util.success(ctx, result);
  } catch (error) {
    console.error('Get vendor list error:', error.message);
    util.fail(ctx, error.message || '查询失败');
  }
}

async function updateVendorState(ctx) {
  const { userId } = util.decodeToken(ctx);
  const { id, state } = ctx.request.body;

  if (!id) {
    return util.fail(ctx, '供应商 ID 无效');
  }
  const newState = state === 1017001 ? 1017002 : 1017001;

  try {
    await VendorService.updateVendorState({ id, newState, updata_id: userId });
    util.success(ctx, '');
  } catch (error) {
    console.error('Update vendor state error:', error.message);
    util.fail(ctx, error.message || '状态更新失败');
  }
}

module.exports = {
  createVendor,
  deleteVendors,
  updateVendor,
  getVendorList,
  updateVendorState
};
